from django.contrib import admin
from .models import Order, OrderItem, OrderStatusHistory, RefundRequest, ShippingAddress


class OrderItemInline(admin.TabularInline):
    model = OrderItem
    extra = 0
    readonly_fields = ['book', 'quantity', 'price', 'subtotal']
    can_delete = False


class OrderStatusHistoryInline(admin.TabularInline):
    model = OrderStatusHistory
    extra = 0
    readonly_fields = ['status', 'note', 'created_at']
    can_delete = False


@admin.register(Order)
class OrderAdmin(admin.ModelAdmin):
    list_display = ['order_number', 'user', 'total_amount', 'status', 'payment_status', 'created_at']
    list_filter = ['status', 'payment_status', 'created_at']
    search_fields = ['order_number', 'user__email', 'user__username']
    readonly_fields = ['order_number', 'created_at', 'updated_at']
    inlines = [OrderItemInline, OrderStatusHistoryInline]
    
    fieldsets = (
        ('基本信息', {
            'fields': ('order_number', 'user', 'status', 'payment_status')
        }),
        ('金额信息', {
            'fields': ('total_amount', 'discount_amount', 'shipping_fee', 'final_amount')
        }),
        ('配送信息', {
            'fields': ('shipping_address', 'tracking_number', 'shipping_method')
        }),
        ('时间信息', {
            'fields': ('created_at', 'updated_at', 'paid_at', 'shipped_at', 'delivered_at')
        }),
        ('备注', {
            'fields': ('notes',),
            'classes': ('collapse',)
        })
    )


@admin.register(OrderItem)
class OrderItemAdmin(admin.ModelAdmin):
    list_display = ['order', 'book', 'quantity', 'price', 'subtotal']
    search_fields = ['order__order_number', 'book__title']
    readonly_fields = ['order', 'book', 'quantity', 'price', 'subtotal']


@admin.register(OrderStatusHistory)
class OrderStatusHistoryAdmin(admin.ModelAdmin):
    list_display = ['order', 'status', 'created_at']
    list_filter = ['status', 'created_at']
    search_fields = ['order__order_number']
    readonly_fields = ['order', 'status', 'note', 'created_at']
    date_hierarchy = 'created_at'


@admin.register(RefundRequest)
class RefundRequestAdmin(admin.ModelAdmin):
    list_display = ['order', 'user', 'status', 'refund_amount', 'created_at']
    list_filter = ['status', 'created_at']
    search_fields = ['order__order_number', 'user__email']
    readonly_fields = ['order', 'user', 'created_at']
    
    fieldsets = (
        ('基本信息', {
            'fields': ('order', 'user', 'status')
        }),
        ('退款信息', {
            'fields': ('refund_amount', 'reason', 'description')
        }),
        ('处理信息', {
            'fields': ('processed_by', 'processed_at', 'admin_note'),
            'classes': ('collapse',)
        }),
        ('时间信息', {
            'fields': ('created_at',),
            'classes': ('collapse',)
        })
    )


@admin.register(ShippingAddress)
class ShippingAddressAdmin(admin.ModelAdmin):
    list_display = ['user', 'name', 'phone', 'province', 'city', 'is_default']
    search_fields = ['user__email', 'name', 'phone']
    list_filter = ['is_default', 'province', 'city']
